﻿using System;
using System.Drawing;
using System.Windows.Forms;

using SuperMap.Data;
using SuperMap.Mapping;
using SuperMap.UI;

namespace SuperMapRadarScanningSample
{
    public partial class MainForm : Form
    {
        // 测试数据
        private string dataPath = "../../data/World/World.smwu";

        private Workspace workspace;
        private MapControl mapControl;
        private GeoRegion radar;

        private System.Timers.Timer timer;

        public MainForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 初始化数据
            WorkspaceConnectionInfo workspaceConnectionInfo = new WorkspaceConnectionInfo()
            {
                Server = dataPath,
                Type = WorkspaceType.SMWU,
                Version = WorkspaceVersion.UGC70
            };
            workspace = new Workspace();
            if (!workspace.Open(workspaceConnectionInfo))
            {
                MessageBox.Show("测试数据工作空间打开失败！请检查数据是否存在异常。", "工作空间打开失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            InitLayouts();

            // 打开地图底图
            mapControl.Map.Workspace = workspace;
            mapControl.Map.Open(workspace.Maps[0]);

            DrawRaderScanning();
        }

        /// <summary>
        /// 绘制雷达扫描效果
        /// </summary>
        private void DrawRaderScanning()
        {
            Rectangle2D mapViewBounds = new Rectangle2D();
            // 雷达范围底图
            if (workspace.Datasources[0].Datasets["RadarBase"] is DatasetVector datasetVectorL)
            {
                mapViewBounds = datasetVectorL.Bounds;
                Recordset recordset = datasetVectorL.GetRecordset(false, CursorType.Static);
                recordset.MoveFirst();
                do
                {
                    Geometry geometry = recordset.GetGeometry();
                    if (geometry == null)
                    {
                        continue;
                    }
                    GeoStyle geoStyle = new GeoStyle
                    {
                        LineColor = Color.Green,
                        LineWidth = 0.1
                    };
                    geometry.Style = geoStyle;
                    mapControl.Map.TrackingLayer.Add(geometry, Guid.NewGuid().ToString());
                } while (recordset.MoveNext());
                recordset.Close();
                recordset.Dispose();
            }

            // 扇形雷达
            if (workspace.Datasources[0].Datasets["Radar"] is DatasetVector datasetVector)
            {
                Recordset recordset = datasetVector.GetRecordset(false, CursorType.Static);
                recordset.MoveFirst();
                Geometry geometry = recordset.GetGeometry();
                recordset.Close();
                recordset.Dispose();

                if (geometry is GeoRegion radar)
                {
                    this.radar = radar;
                    GeoStyle geoStyle = new GeoStyle
                    {
                        FillForeColor = Color.White,
                        FillBackColor = Color.Green,
                        FillGradientMode = FillGradientMode.Linear,
                        FillGradientAngle = 270,
                        FillOpaqueRate = 50,
                        LineSymbolID = 5
                    };
                    radar.Style = geoStyle;

                    Map map = mapControl.Map;
                    map.TrackingLayer.Add(radar, "radar");
                    map.ViewBounds = mapViewBounds;
                    map.RefreshTrackingLayer();
                }
            }
        }

        /// <summary>
        /// 定时器刷新实现雷达动态效果
        /// </summary>
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            radar.Style.FillGradientAngle -= 1;
            if (radar.Style.FillGradientAngle <= -0)
            {
                radar.Style.FillGradientAngle = 360;
            }
            radar.Rotate(new Point2D(104.431623223191, 34.4962593960899), -1);
            Map map = mapControl.Map;
            int index = map.TrackingLayer.IndexOf("radar");
            map.TrackingLayer.Set(index, radar);
            map.RefreshTrackingLayer();
        }

        /// <summary>
        /// 控件布局
        /// </summary>
        private void InitLayouts()
        {
            startLbl.Enabled = true;
            stopLbl.Enabled = false;

            mapControl = new MapControl()
            {
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top,
                Bounds = new Rectangle(0, 0, ClientRectangle.Width, ClientRectangle.Height)
            };
            this.Controls.Add(mapControl);
        }

        /// <summary>
        /// 启动雷达扫描动画
        /// </summary>
        private void startLbl_Click(object sender, EventArgs e)
        {
            if (timer == null)
            {
                timer = new System.Timers.Timer(5);
                timer.Elapsed += Timer_Elapsed;
            }
            timer.Start();
            startLbl.Enabled = false;
            stopLbl.Enabled = true;
        }

        /// <summary>
        /// 暂停雷达扫描动画
        /// </summary>
        private void stopLbl_Click(object sender, EventArgs e)
        {
            if (timer != null)
            {
                timer.Stop();
                stopLbl.Enabled = false;
                startLbl.Enabled = true;
            }
        }
    }
}
